package cn.itsource.ymcc.producer;

import cn.itsource.ymcc.constant.OrderStatusConstants;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.result.JsonResult;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class OrderMessageProducerImpl implements OrderMessageProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public JsonResult syncSend(CourseOrder courseOrder) {
        //设置 主题:标签
        rocketMQTemplate.sendOneWay(
                OrderStatusConstants.TOPIC_COURSE_ORDER_PUSH_TO_SRS + ":" + OrderStatusConstants.TAGS_COURSE_ORDER_PUSH_TO_SRS,
                //我们要传 字符串
                MessageBuilder.withPayload(JSON.toJSONString(courseOrder)).build()
        );
        return JsonResult.success();
    }

    @Override
    public JsonResult syncSendDelay(CourseOrder courseOrder) {
        //延迟3    10s
        rocketMQTemplate.syncSend(OrderStatusConstants.TOPIC_COURSE_ORDER_DELAY+":"+OrderStatusConstants.TAGS_COURSE_ORDER_DELAY,
                MessageBuilder.withPayload(JSON.toJSONString(courseOrder)).build(),
                2000,
                3);
        return JsonResult.success();
    }

    @Override
    public JsonResult syncSendDelayCloseOrder(String orderNo) {
        //延迟3    10s
        rocketMQTemplate.syncSend(OrderStatusConstants.TOPIC_COURSE_ORDER_DELAY_CLOSE+":"+OrderStatusConstants.TAGS_COURSE_ORDER_DELAY_CLOSE,
                MessageBuilder.withPayload(orderNo).build(),
                2000,
                3);
        return JsonResult.success();
    }
}
